﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using P2P_Backup.archive;

namespace P2P_Backup.forms.controls
{
    public class ArchiveTreeView : TreeView
    {
        private Archive archive;
        public Archive Archive
        {
            set
            {
                this.archive = value;
                this.Nodes.Clear();
                this.Nodes.Add(new FolderNode(value));                
            }
        }
        
        public Folder getSelectedFolder(bool FindParent = false)
        {
            if (this.SelectedNode is FileNode || FindParent)// als ge een file hebt aangeduid word het in de parent folder toegevoegd
            {
                FolderNode node = (FolderNode)this.SelectedNode.Parent;
                return node.getFolder();
            }
            else if (this.SelectedNode is FolderNode)
            {
                FolderNode node = (FolderNode)this.SelectedNode;
                return node.getFolder();
            }

            return this.archive;
        }
        public void RemoveSelectedNode()
        {
            if (SelectedNode.Parent != null)
            {
                if (SelectedNode is FolderNode)
                {
                    Folder f = ((FolderNode)SelectedNode).getFolder();
                    getSelectedFolder(true).removeFolder(f);
                }
                else if (SelectedNode is FileNode)
                {
                    File f = ((FileNode)SelectedNode).getFile();
                    getSelectedFolder(false).removeFile(f);
                    SelectedNode.Remove();
                }
            }
            else
            {
                throw new Exception("Don't try to remove the root");
            }
        }
    }
}
